package cn.net.chenbao.medical.adapter.radapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

/**
 * Created by Bubble on 2016/8/23 0023.
 */
public abstract class RvCommonAdapter<T> extends RecyclerView.Adapter<CommonViewHolder> {

    protected LayoutInflater mInflater;
    protected Context mContext;
    protected List<T> mdatas;
    protected int mLayoutId;
    public onItemClickListener  mClickListener;


    public interface  onItemClickListener{
        void onItemClick(View view, int position);
//        void onItemLongClick(View view, int position);
    }

    public void setOnItemClickListener( onItemClickListener  listener){

        mClickListener=listener;
    }

    public void setDatas(List<T> datas) {
        this.mdatas = datas;
    }

    public RvCommonAdapter(Context mContext, List<T> mdatas, int mLayoutId) {
        this.mContext = mContext;
        this.mdatas = mdatas;
        this.mLayoutId = mLayoutId;
        mInflater=LayoutInflater.from(mContext);
    }

    @Override
    public CommonViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView=mInflater.inflate(mLayoutId,parent,false);
        return new CommonViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(final CommonViewHolder holder, final int position) {
        holder.setItem(mdatas.get(position));
        convert(holder,mdatas.get(position));
        final int layoutPosition=holder.getLayoutPosition();
        if(mClickListener!=null){
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                    mClickListener.onItemClick(holder.itemView,position);

            }
        });
        }
    }

    @Override
    public int getItemCount() {
        return mdatas.size();
    }



    public abstract void convert(CommonViewHolder holder, T t);

    public T getItem(int position){
        return mdatas.get(position);
    }

}
